package com.dlg.commons.util;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * 对象转化为Map 
 * @author mwz
 * @since  2017年5月22日 下午4:33:35
 */
public class ObjectConverterMapUtils {
	
	public static Map<String, String> conver(Object obj){
		Map<String, String> map = new HashMap<String, String>();
		try {
			BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
			PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
			for (PropertyDescriptor property : propertyDescriptors) {    
	            String key = property.getName();    
	            if (key.compareToIgnoreCase("class") == 0) {   
	                continue;  
	            }  
	            Method getter = property.getReadMethod();  
	            Object value = getter != null ? getter.invoke(obj) : null;  
	            map.put(key, value == null ? "" : value + "");  
	        }
		} catch (Exception e) {
			e.printStackTrace();
		}  
		return map;
	}

}
